package com.example.controller;

import com.example.common.Result;
import com.example.entity.Customer;
import com.example.entity.PageBean;
import com.example.entity.Worker;
import com.example.service.CustomerService;
import com.example.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize, String name
                      ) {
        PageBean pageBean = customerService.page(page, pageSize, name);
        return Result.success(pageBean);
    }
    @PostMapping("/login")
    public Result login(@RequestBody Customer customer) {
        Customer customer1 = customerService.login(customer);
        if (customer1 != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", customer1.getCustomerId());
            String jwt = JwtUtils.generateJwt(claims);
            Map<String, Object> data = new HashMap<>();
            data.put("token", jwt);
            return Result.success(data);
        } else {
            return Result.error("用户名或密码错误！");
        }
    }
    @GetMapping("/info")
    public Result getuUserinfo(@RequestParam("token") String token) {
        Map<String, Object> data = customerService.getuUserinfo(token);
        if (data != null) {
            return Result.success(data);
        }
        return Result.error("token出错");
    }
    //驼峰命名法w_name无法实现映射，
    @PostMapping
    public Result add(@RequestBody Customer customer) {
        log.info("添加顾客的信息为：{}", customer);
        customerService.add(customer);
        return Result.success();
    }
    @PostMapping("/addVisitor")
    public Result addVisitor() {
        Customer customer = customerService.addVisitor();
        return Result.success(customer);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("要删除顾客的id为：{}", ids);
        customerService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查找顾客的id为：{}", id);
        Customer customer = customerService.findById(id);
        return Result.success(customer);
    }

    @PutMapping("/updata")
        public Result updata(@RequestBody Customer customer) {
        log.info("更新的顾客信息为：{}", customer);
        customerService.updata(customer);
        return Result.success();
    }
}
